<template>
  <div>
    <div class="login_top clearfix">
      <a href="/" class="login_logo">
        <img src="../assets/images/logo02.png" alt="" />
      </a>
    </div>

    <div class="login_form_bg">
      <div class="login_form_wrap clearfix">
        <div class="login_banner fl"></div>
        <div class="slogan fl">日夜兼程 · 急速送达</div>
        <div class="login_form fr">
          <div class="login_title clearfix">
            <h1>用户登录</h1>
            <a href="/register">立即注册</a>
          </div>

          <form class="form_input">
            <div>
              <input class="name_input" placeholder="请输入您的用户名" v-model="user.username" @keypress="prevent" />

              <input class="pass_input" placeholder="请输入密码" v-model="user.password" @keypress="prevent" type="password" />

              <div class="more_input clearfix">
                <input type="checkbox" checked />
                <label>记住用户名</label>
                <a href="javascript:void(0)">忘记密码</a>
              </div>

              <button type="button" class="input_submit" @click="login">登录</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {storage} from "@/assets/js/localStorage";
  import {error, postForm} from "@/assets/js/axiosUtis";

  export default {
    data() {
      return {
        user: {
          username: "",
          password: ""
        }
      }
    },
    methods: {
      prevent() {
        let event = window.event;

        if (event.keyCode === 32) {
          event.returnValue = false;
        }
      },
      login() {
        let username = this.user.username;
        let password = this.user.password;

        if (!username) {
          alert("请输入用户名~");
        } else if (!password) {
          alert("请输入密码~");
        } else {
          postForm("/user/login", this.user, function (res) {
            storage("token", res.data.data);

            location.href = "/";
          }, function (err) {
            error(err);
          });
        }
      }
    },
    name: "LoginView",
  }
</script>